
const User_base = require('User_base')

class User
{
    constructor()
    {
        this.mData = {}

        this.mTowers = {}
    }

    close()
    {
        if (this.mCacheData)
        {
            this.mCacheData = null
        }
    }

    sysncContent(d)
    {
        if (!d)
        {
            return
        }
  
        for (let k in d)
        {
            this.mData[k] = d[k]
        }
    }

    contentBy(p)
    {
        if (p)
        {
            let d = this.mData[p.key]
            if (d != null && d != undefined)
            {
                return d
            }

            return p.def
        }
        return null
    }

    setContentBy(p, value)
    {
        if (p)
        {
            this.mData[p.key] = value
        }
    }

    read(p)
    {
        if (!this.mCacheData)
        {
            const id = this.contentBy(User_base.id)
            if (!id)
            {
                return p.def
            }

            try
            {
                let d2 = cc.sys.localStorage.getItem('user' + id + ".ptd")
                this.mCacheData = d2 ? JSON.parse(d2) : {}
            }
            catch(e)
            {
                this.mCacheData = {}
            }
        }

        let d = this.mCacheData[p.key]
        if (d != null && d != undefined)
        {
            return d
        }

        return p.def
    }

    write(p, v, s)
    {
        if (!this.mCacheData)
        {
            return false
        }

        this.mCacheData[p.key] = v

        if (s)
        {
            const id = this.contentBy(User_base.id)
            if (!id)
            {
                return false
            }
            cc.sys.localStorage.setItem('user' + id + ".ptd", JSON.stringify(this.mCacheData))
        }

        return true
    }

    updateAvatarUrl()
    {
        
    }
}

export default User
